package com.bjsxt.datastructure.linetable;


import java.util.Arrays;

/**
 * 顺序表，底层采用的数组，但是长度可以动态的变化
 */
public class ArrayList implements List {

    private Object[] elementData;//底层是一个数组
    private int size;//不是数字分配的几个空间，而是元素的个数


    public ArrayList() {
        //没有指定长度，默认长度4
        this(4);
//        elementData = new Object[]{};
    }

    /**
     * 数组的初始长度
     * @param initialCapacity
     */
    public ArrayList(int initialCapacity) {
        elementData = new Object[initialCapacity];
        size = 0;
    }


    @Override
    public int size() {
        return size;
    }

    @Override
    public Object get(int i) {
        if (i < 0 || i >= size) {
//            throw new RuntimeException("数组索引越界异常：" + i);
            throw new MyArrayIndexOutOfBoundsException("数组索引越界异常：" + i);
        }
        return elementData[i];
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(Object e) {
        return false;
    }

    @Override
    public int indexOf(Object e) {
        return 0;
    }

    @Override
    public void add(int i, Object e) {
        if (i < 0 || i > size) {
            throw new MyArrayIndexOutOfBoundsException("数组索引越界异常：" + i);
        }
        //如果数组满了，就扩容
        if (size == elementData.length) {
            grow();
        }
        //后移i及其后面的元素,从最后一个元素开始
        for (int j = size; j > i; j--) {
            elementData[j] = elementData[j - 1];
        }
        //给数组第i个元素赋值
        elementData[i] = e;
        //元素个数+1
        size++;
    }

    @Override
    public void add(Object e) {

//        //如果数组满了，就扩容
//        if (size == elementData.length) {
//            grow();
//        }
//        //给数组赋值
//        elementData[size] = e;
//        //元素个数+1
//        size++;
////        elementData[size++] = e;
////        System.out.println("length" + elementData.length);

        this.add(size, e);
    }

    private void grow() {
//            //新创建要给新的数组，长度是旧数组的2倍
//            Object [] newArr = new Object[elementData.length * 2];
//            //讲旧数组的数据拷贝到新数组
//            for (int i = 0; i < elementData.length; i++) {
//                newArr[i] = elementData[i];
//            }
//            //让elementData指向新数组
//            elementData = newArr;

        //其实用这一条语句就够了
        elementData = Arrays.copyOf(elementData, elementData.length * 2);
    }

    @Override
    public boolean addBefore(Object obj, Object e) {
        return false;
    }

    @Override
    public boolean addAfter(Object obj, Object e) {
        return false;
    }

    @Override
    public void remove(int i) {
        if (i < 0 || i > size) {
            throw new MyArrayIndexOutOfBoundsException("数组索引越界异常：" + i);
        }
        for (int j = i; j < size; j++) {
            elementData[j] = elementData[j+1];
        }
        size--;
    }

    @Override
    public boolean remove(Object e) {
        int index = -1;
        for (int i = 0; i < size; i++) {
            if (elementData[i].equals(e)) {
                index = i;
            }
        }
        if (index > -1) {
            this.remove(index);
            return true;
        }
        return false;
    }

    @Override
    public Object replace(int i, Object e) {
        if (i < 0 || i > size) {
            throw new MyArrayIndexOutOfBoundsException("数组索引越界异常：" + i);
        }
        elementData[i] = e;
        return null;
    }

    @Override
    public String toString() {
        if (size == 0) {
            return "[]";
        }
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < size; i++) {
            if (i != size - 1) {
                builder.append(elementData[i] + ",");
            } else {
                builder.append(elementData[i]);
            }
        }
        builder.append("]");
        return builder.toString();
//        return "ArrayList{" +
//                "elementData=" + Arrays.toString(elementData) +
//                ", size=" + size +
//                '}';
    }
}
